home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLGR106.ARJ / MOUSE.C < prev    next >
C/C++ Source or Header  |  1992-03-29  |  4KB  |  170 lines

  1. /* This is file MOUSE.C */
  2. /*
  3. ** Copyright (C) 1991 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include "mouse.h"
  17. #include "sys/registers.h"
  18. #include "ptr16.h"
  19.  
  20. static int mouse_initted = 0;
  21. static MouseEvent last_event;
  22. static unsigned char old_pixels[ptr16_height*ptr16_width];
  23. static int lastx, lasty;
  24. static mouse_fg, mouse_bg;
  25.  
  26. void MouseSetColors(int fg, int bg)
  27. {
  28.   mouse_fg = fg;
  29.   mouse_bg = bg;
  30. }
  31.  
  32. static paint_mouse(x, y)
  33. {
  34.   int dx, dy, o;
  35.   for (dx=0; dx<ptr16_width; dx++)
  36.     for (dy=0; dy<ptr16_height; dy++)
  37.     {
  38.       o = dx + dy * ptr16_width;
  39.       switch (ptr16_data[o])
  40.       {
  41.         case 1:
  42.           old_pixels[o] = GrPixel(x+dx, y+dy);
  43.           GrPlot(x+dx, y+dy, mouse_bg);
  44.           break;
  45.         case 2:
  46.           old_pixels[o] = GrPixel(x+dx, y+dy);
  47.           GrPlot(x+dx, y+dy, mouse_fg);
  48.           break;
  49.       }
  50.     }
  51. }
  52.  
  53. static unpaint_mouse()
  54. {
  55.   int dx, dy, o;
  56.   for (dx=0; dx<ptr16_width; dx++)
  57.     for (dy=0; dy<ptr16_height; dy++)
  58.     {
  59.       o = dx + dy * ptr16_width;
  60.       if (ptr16_data[o])
  61.         GrPlot(lastx+dx, lasty+dy, old_pixels[o]);
  62.     }
  63. }
  64.  
  65. void MouseSetSpeed(int speed)
  66. {
  67.   REGISTERS r;
  68.   r.ax = 15;
  69.   r.cx = speed;
  70.   r.dx = speed;
  71.   int33(&r);
  72. }
  73.  
  74. void MouseWarp(int x, int y)
  75. {
  76.   REGISTERS r;
  77.   r.ax = 4;
  78.   r.cx = last_event.x = x;
  79.   r.dx = last_event.y = y;
  80.   int33(&r);
  81. }
  82.  
  83. void MouseGetEvent(int flags, MouseEvent *e)
  84. {
  85.   REGISTERS r;
  86.   if (!mouse_initted)
  87.   {
  88.     mouse_initted = 1;
  89.     r.ax = 0;
  90.     int33(&r);
  91.     r.ax = 7;
  92.     r.cx = 0;
  93.     r.dx = GrMaxX();
  94.     int33(&r);
  95.     r.ax = 8;
  96.     r.cx = 0;
  97.     r.dx = GrMaxY();
  98.     int33(&r);
  99.     MouseWarp(GrMaxX()/2, GrMaxY()/2);
  100.     r.ax = 3;
  101.     int33(&r);
  102.     last_event.x = r.cx;
  103.     last_event.y = r.dx;
  104.     last_event.buttons = r.bx & 7;
  105.     MouseSetSpeed(8);
  106.     MouseSetColors(GrWhite(), GrBlack());
  107.   }
  108.   if (!(flags & M_NOPAINT))
  109.     paint_mouse(last_event.x, last_event.y);
  110.   lastx = last_event.x;
  111.   lasty = last_event.y;
  112.   e->flags = 0;
  113.   while (1)
  114.   {
  115.     r.ax = 3;
  116.     int33(&r);
  117.     e->buttons = r.bx & 7;
  118.     r.ax = 11;
  119.     int33(&r);
  120.     e->x = lastx + (short)r.cx;
  121.     e->y = lasty + (short)r.dx;
  122.     if (e->x < 0) e->x = 0;
  123.     if (e->x > GrMaxX()) e->x = GrMaxX();
  124.     if (e->y < 0) e->y = 0;
  125.     if (e->y > GrMaxY()) e->y = GrMaxY();
  126.     if (flags & M_KEYPRESS)
  127.       if (kbhit())
  128.       {
  129.         e->key = getkey();
  130.         e->flags |= M_KEYPRESS;
  131.       }
  132.     if ((e->x != lastx) || (e->y != lasty))
  133.     {
  134.       if (!(flags & M_NOPAINT))
  135.       {
  136.         unpaint_mouse();
  137.         paint_mouse(e->x, e->y);
  138.         lastx = e->x;
  139.         lasty = e->y;    
  140.       }
  141.       e->flags |= M_MOTION;
  142.     }
  143.     if (e->buttons != last_event.buttons)
  144.     {
  145.       int up = (~e->buttons & last_event.buttons);
  146.       int down = (e->buttons & ~last_event.buttons);
  147.       if (up & M_LEFT)
  148.         e->flags |= M_LEFT_UP;
  149.       if (up & M_MIDDLE)
  150.         e->flags |= M_MIDDLE_UP;
  151.       if (up & M_RIGHT)
  152.         e->flags |= M_RIGHT_UP;
  153.       if (down & M_LEFT)
  154.         e->flags |= M_LEFT_DOWN;
  155.       if (down & M_MIDDLE)
  156.         e->flags |= M_MIDDLE_DOWN;
  157.       if (down & M_RIGHT)
  158.         e->flags |= M_RIGHT_DOWN;
  159.     }
  160.     e->flags |= (flags & M_POLL);
  161.     if (e->flags & flags)
  162.       break;
  163.   }
  164.   e->flags &= flags;
  165.   last_event = *e;
  166.   last_event.flags &= ~M_KEYPRESS;
  167.   if (!(flags & M_NOPAINT))
  168.     unpaint_mouse();
  169. }
  170.